#
# Copyright 2010 Steve Leach
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#

import urllib
import logging
from xml.dom.minidom import parseString

from google.appengine.api import urlfetch

from eveapi import EVEAPIConnection

from handlers import BaseHandler

def add_mappings(mappings):
	mappings.append(('/api_info', ApiInfoHandler))

class ApiCredentials:
	pass

class EveCharacter:
	def parse_summary(self, node):
		self.name = node.getAttribute('name')
		self.code = node.getAttribute('characterID')
		self.corp_name = node.getAttribute('corporationName')
		self.corp_id = node.getAttribute('corporationID')

	def parse_char_sheet(self, text):
		logging.info(text)
		dom = parseString(text)

		self.attrs = {}
		attr_nodes = dom.getElementsByTagName('attributes')[0].childNodes
		for node in attr_nodes:
			if not node.nodeType == node.TEXT_NODE:
				attr_name = node.nodeName					
				value = int(node.firstChild.data)
				self.attrs[attr_name] = value

class ApiInfoHandler(BaseHandler):
	def do_get(self):
		self.do_post()


	def do_post(self):
		api = EVEAPIConnection()
		auth = api.auth(userID=self.request.get('account'), apiKey=self.request.get('key'))
		logging.info(auth)

		self.pagevalues['account'] = self.request.get('account')
		self.pagevalues['key'] = self.request.get('key')

		if self.request.get('char'):
			char = auth.character(self.request.get('char'))
			logging.info(char)
			char_sheet = char.CharacterSheet()
			self.pagevalues['charName'] = char_sheet.name
			self.pagevalues['charSheet'] = char_sheet
			logging.info(char_sheet.name)
			self.show_page('api_char_info.html')
		else:
			self.pagevalues['chars'] = auth.account.Characters().characters
			self.show_page('api_account_info.html')

# end of file

